package com.example.home.me;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.bean.Content;
import com.example.home.tool.CotextToken;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

public class FeedBackActivity extends AppCompatActivity {

    private EditText feedbackEditText;
    private Button submitButton;
    private Gson gson;
    private Content content;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);

        initView();
        submitFeedback();
    }

    private void initView() {
        feedbackEditText = findViewById(R.id.feedbackEditText);
        submitButton = findViewById(R.id.submitButton);
        gson = new Gson();
        handler = new Handler();
    }

    private void submitFeedback() {
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String feedback = feedbackEditText.getText().toString();
                if (feedback.trim().isEmpty()) {
                    Toast.makeText(FeedBackActivity.this, "请输入反馈内容", Toast.LENGTH_SHORT).show();
                } else if (feedback.length() > 150) {
                    Toast.makeText(FeedBackActivity.this, "反馈内容超过150个字", Toast.LENGTH_SHORT).show();
                } else {
                    sendFeedback(feedback);
                }
            }
        });
    }

    private void sendFeedback(String feedback) {
        OKHttpTool.doNetRequest("prod-api/api/common/feedback",
                OKHttpTool.POST,
                Content.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        content = (Content) obj;
                        CotextToken.TOKEN  = content.getToken();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(FeedBackActivity.this, "反馈已提交", Toast.LENGTH_SHORT).show();
                                FeedBackActivity.this.finish();
                            }
                        });
                    }
                    @Override
                    public void onFailure(String msg) {
                        Log.v("xhf", "反馈失败");
                    }
                }
        );
    }
}
